package com.penghaisoft.microwms.materialhand.web.insidewarehouse.controller;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.HandBaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.RawStorageSnDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.materialhand.web.insidewarehouse.service.IMaterialHandInsideWhService;

/** 
* @ClassName: MoveMaterialController 
* @Description: 移库任务
* @author jzh
* @date 2018年7月2日 上午10:01:12  
*/
@RestController
@RequestMapping("insideWh/move")
public class MoveMaterialController extends HandBaseController {
	
	@Autowired
    private IMaterialHandInsideWhService materialHandInsideWhService;
	
    /** 
    * @Title: moveFig 
    * @Description: 判断该条码是否可移库，
    * @param @param barcode
    * @param @return  参数说明 
    * @return HandResp    返回类型
    * @author jzh 
    * @throws 
    */
    @PostMapping("moveconfig")
    public HandResp moveConfig(@RequestBody RawStorageSn rawStorageSn){
        HandResp result = new HandResp();
        //Code先置为1SUCCESS
        result.setCode(Constant.SUCCESS);
        //根据条码查询
        rawStorageSn.setActiveFlag("1");
        List<RawStorageSn> list = materialHandInsideWhService.querybarcodeExist(rawStorageSn);
        if(list.size() <= 0){
        	result.setMsg("该条码不存在");
        	result.setCode(Constant.FAIL);
        }else{
        	if(list.get(0).getBarcodeStatus().equals("2")){
        		result.setMsg("该条码已出库");
            	result.setCode(Constant.FAIL);
        	}else{
        		//返回信息
        		result.setList(list);
        		result.setMsg("该条码可进行移库，请选择库位");
        	}
        }
        return result;
    }
    
    /** 
    * @Title: moveWl 
    * @Description: 移库 
    * @param @param rawStorageSnDto
    * @param @return  参数说明 
    * @return HandResp    返回类型
    * @author jzh 
    * @throws 
    */
    @PostMapping("wlcode")
    public HandResp moveWl(@RequestBody RawStorageSnDto rawStorageSnDto){
        HandResp result = new HandResp();
        //Code先置为1SUCCESS
        result.setCode(Constant.SUCCESS);
        RawStorageSn barcodeStorageSn = new RawStorageSn();
        barcodeStorageSn.setBarcode(rawStorageSnDto.getBarcode());
        barcodeStorageSn.setActiveFlag("1");
        //根据条码查询
        List<RawStorageSn> list = materialHandInsideWhService.querybarcodeExist(barcodeStorageSn);
        if(list.size() <= 0){
        	result.setMsg("该条码不存在");
        	result.setCode(Constant.FAIL);
        }else{
        	if(list.get(0).getBarcodeStatus().equals("2")){
        		result.setMsg("该条码已出库");
            	result.setCode(Constant.FAIL);
        	}else{
        		if(list.get(0).getWlCode().equals(rawStorageSnDto.getWlCode())){
        			result.setMsg("重新选择库位，新库位与原库位一致");
                	result.setCode(Constant.FAIL);
        		}else{
        			//开始移库
        			Resp resp = new Resp();
        			RawStorageSn rawStorageSn = new RawStorageSn();
        			rawStorageSn.setBarcode(rawStorageSnDto.getBarcode());
        			rawStorageSn.setWlCode(rawStorageSnDto.getWlCode());
        			rawStorageSn.setLastModifiedBy(rawStorageSnDto.getLoginName());
        			rawStorageSn.setGmtModified(new Date());
        			resp = materialHandInsideWhService.updateRawStorageSnWl(rawStorageSn);
        			result.setCode(resp.getCode());
        			if(resp.getCode().equals("2")){
        				result.setMsg(resp.getMsg());	
        			}
        		}
        	}
        }
        return result;
    }

}
